import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let lifeData = {};

try {
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = $cookies.get('lifeData') ? $cookies.get('lifeData') : {};

} catch (e) {

}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名

// 保存变量到本地lifeData存储中
const saveUserData = function(key, value, time) {
	if (!time) {
		time = '0';
	}
	let tmp = $cookies.get('lifeData') ? $cookies.get('lifeData') : {};
	tmp[key] = value;
	$cookies.set('lifeData', JSON.stringify(tmp), time);
}
const store = new Vuex.Store({
	state: {
		// 如果上面从本地获取的lifeData对象下有对应的属性，就赋值给state中对应的变量
		// 加上vuex_前缀，是防止变量名冲突，也让人一目了然
		vuex_user: lifeData.vuex_user ? lifeData.vuex_user : null,
		// menu:lifeData.menu ? lifeData.menu : [],
		tagsList: JSON.parse($cookies.get('tagsList')) ? JSON.parse($cookies.get('tagsList')) : [],
		user: lifeData.user ? lifeData.user : false,
		url: 'http://127.0.0.1:10004',
		bgtype: lifeData.bgtype ? lifeData.bgtype : '#242f42', //背景主题
		texttype: lifeData.texttype ? lifeData.texttype : '#fff', //字体主题
		activetexttype: lifeData.activetexttype ? lifeData.activetexttype : '#20a0ff', //选中字体
		collapses: lifeData.collapses,
		jigoulst:[],//机构列表
		loudonglst:[],//楼栋列表
		loucenlst:[],//楼层列表
		loufanglst:[],//房间列表
		louweilst:[],//床位列表
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if (len >= 2) {
				let obj = state[nameArr[0]];
				for (let i = 1; i < len - 1; i++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveUserData(saveKey, state[saveKey], payload.time)
		},
		
		getjigou(state,obj) {
			$axios({
				method: 'get',
				url: '/api/v1/Company/Tree',
				params: {
					id: 0
				}, //get参数
				// data:{id:o.id}//post参数
			}).then((res) => {
				// console.log(res)
				if (res.state == 200) {
					state.jigoulst = res.items ? [res.items] : [];
				} else {
					obj.$message.error('获取机构失败！');
				}
			}).catch((error) => {
			});
		},
		getloudong(state){
			$axios({
				method: 'get',
				url: '/api/v1/CompanyBuilding/GetList?Key=&PageSize=0&PageIndex=1&total=0',
				// params: this.query, //get参数
				// data:{id:o.id}//post参数
			}).then((res) => {
				if(res.state == 200){
					state.loudonglst = res.items?res.items:[];
				}else{
				}
			}).catch((error)=> {
			});
		},
		getloucen(state){
			$axios({
				method: 'get',
				url: '/api/v1/CompanyArea/GetList?Level=0',
			}).then((res) => {
				if(res.state == 200){
					state.loucenlst = res.items?res.items:[];
				}else{
				}
			}).catch((error)=> {
			});
		},
		getfangcen(state){
			$axios({
				method: 'get',
				url: '/api/v1/CompanyArea/GetList?Level=1',
			}).then((res) => {
				if(res.state == 200){
					state.loufanglst = res.items?res.items:[];
					// console.log(state.loufanglst)
				}else{
				}
			}).catch((error)=> {
			});
		},
		getweicen(state){
			$axios({
				method: 'get',
				url: '/api/v1/CompanyArea/GetList?Level=2',
			}).then((res) => {
				if(res.state == 200){
					state.louweilst = res.items?res.items:[];
				}else{
				}
			}).catch((error)=> {
			});
		}
	},
	getters: {
		newlst(state) {
			var setarr = function(obj){
				for(var i in obj){
					let o = obj[i];
					if(o.child && !o.child.length){
						delete o.child;
					}else{
						setarr(o.child)
					}
				}
			}
			setarr(state.jigoulst);
			return state.jigoulst;
		}
	},
	actions: {
		
	}
})

export default store
